/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package isratrzabality.controller;

import isratrzabality.datalayer.Artefact;
import isratrzabality.datalayer.DataSourceMemory;
import isratrzabality.uilayer.TrazaResultGrid;
import isratrzabality.uilayer.TrazabilityTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.ItemListener;
import java.util.Arrays;
import java.util.HashSet;
import java.util.TreeSet;

/**
 *
 * @author Israel Ruiz
 */
public class TrazaLevelController {

    TrazaResultGrid resultData;
    private static TrazaLevelController singleInstance = new TrazaLevelController();

    public static TrazaLevelController getInstanceController() {
        return singleInstance;
    }

    private TrazaLevelController() {

    }

    public class EventDropDown1 implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent evt) {
            javax.swing.JComboBox jComboBox1 = (javax.swing.JComboBox) evt.getSource();
            int indx = jComboBox1.getSelectedIndex();
            setListDrop2(indx);
            setListDrop3(indx);
        }

        public void elementTrace(ActionEvent evt) {
            int indx1 = resultData.getjComboBox1().getSelectedIndex();
            int indx2 = resultData.getjComboBox2().getSelectedIndex();
            int indx3 = resultData.getjComboBox3().getSelectedIndex();
            setTrazabilityLevel(indx1,indx2,indx3);
            
        }

    }

    public String[] getLevels() {
        Map<String, List<Artefact>> dataSource = DataSourceMemory.getArftefactMemory();
        List<String> levels = new ArrayList<String>(dataSource.keySet());
        Collections.sort(levels);
        return levels.toArray(new String[]{""});

    }

    public void setListDrop2(int indx) {
        String keyMap = (String) resultData.getjComboBox1().getItemAt(indx); // Key Name
        Map<String, List<Artefact>> dataSource = DataSourceMemory.getArftefactMemory();
        List<Artefact> artefacts = dataSource.get(keyMap);
        DefaultComboBoxModel<Artefact> listnames = new DefaultComboBoxModel<Artefact>(getArtefacts(artefacts));
        resultData.getjComboBox2().setModel(listnames);
    }

    private void setListDrop3(int indx) {
        List<String> levels = new ArrayList(Arrays.asList(getLevels()));
        levels.remove(indx);
        DefaultComboBoxModel<String> listlavels = new DefaultComboBoxModel<String>(levels.toArray(new String[]{}));
        resultData.getjComboBox3().setModel(listlavels);
    }

    public Artefact[] getArtefacts(List<Artefact> artefacts) {

        return artefacts.toArray(new Artefact[]{});

    }

    private synchronized void setTrazabilityLevel(int indx1,int indx2,int indx3) {
        String keyBase = (String) resultData.getjComboBox1().getItemAt(indx1);  // Key Base to Trace
        Artefact objArt = (Artefact) resultData.getjComboBox2().getItemAt(indx2); // Artefact
        String lavelName = (String) resultData.getjComboBox3().getItemAt(indx3); // level Trace
        String[] levels = getLevels();
        // Get index Level
        for(int i = 0; i <levels.length; i++){
            if(lavelName.equals(levels[i])){
                indx3 = i;
            }
        }
        java.util.Set<Artefact> initList= new TreeSet<Artefact>();
        java.util.Set<Artefact> auxList= new TreeSet<Artefact>();
        
        if(indx1 > indx3){ // Up
           indx1 = --indx1;
            initList.addAll(objArt.returnListUp());
            for(;initList.size()>0 && indx1> indx3;indx1--){
                for(Artefact artefact :initList){
                    if(artefact.returnListUp() != null){
                        auxList.addAll(artefact.returnListUp());
                    }
                }
                initList = new TreeSet<Artefact>(auxList);
                auxList  = new TreeSet<Artefact>();
                
            }
        }else{ //Down
            indx1 = ++indx1;
            initList.addAll(objArt.returnListDown());
            for(;initList.size()>0 && indx1<indx3;indx1++){
                for(Artefact artefact :initList){
                    if(artefact.returnListDown() != null){
                        auxList.addAll(artefact.returnListDown());
                    }
                }
                initList = new TreeSet<Artefact>(auxList);
                auxList  = new TreeSet<Artefact>();
            }

        }
        
        String[][] dataGrid = new String[initList.size()][3];
        int indx = 0;
        for(Artefact artTrace :initList){
            dataGrid[indx++]= new String[]{artTrace.getId(),artTrace.getName(),artTrace.returnListUp()!= null?artTrace.returnListUp().toString():""};
        }
        
        TrazabilityTableModel model = (TrazabilityTableModel)resultData.getjTable2().getModel();
        model.setData(dataGrid);
        resultData.getjTable2().repaint();
    }

    public void showTrazabilitySearch() {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                resultData = new TrazaResultGrid();
                DefaultComboBoxModel<String> listLevel = new DefaultComboBoxModel<String>(getLevels());
                resultData.getjComboBox1().setModel(listLevel);
                int indx = 0;
                setListDrop2(indx); // Init the screen
                setListDrop3(indx); // Init the screen
                setTrazabilityLevel(indx,indx,indx);
                resultData.setVisible(true);

            }


        });
    }

}
